package bb.lanxing.device.sgdevcie.utils

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import bb.lanxing.util.Log
import kotlinx.coroutines.Job
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat
import no.nordicsemi.android.support.v18.scanner.ScanCallback
import no.nordicsemi.android.support.v18.scanner.ScanResult
import no.nordicsemi.android.support.v18.scanner.ScanSettings
import kotlin.jvm.internal.Intrinsics

@SuppressLint("MissingPermission")
class BleCompatScannerUtil(
    targetAddress: String,
    j: Long,
    private val foundCallback: (BluetoothDevice) -> Unit,
    private val notFoundCallback: (() -> Unit)? = null
) {
    private val duration: Long
    var isFound: Boolean = false
    var jobCancel: Job? = null
    private val scanCallback: BleScanCallback
    private val scanner: BluetoothLeScannerCompat
    private val settings: ScanSettings
    private val targetAddress: String

    init {
        this.targetAddress = targetAddress
        this.duration = j
        val scanner = BluetoothLeScannerCompat.getScanner()
        this.scanner = scanner
        val build = ScanSettings.Builder().setLegacy(false).setScanMode(1)
            .setUseHardwareBatchingIfSupported(true).build()
        this.settings = build
        this.scanCallback = object : BleScanCallback() {
            override fun onScanResult(i: Int, result: ScanResult) {
                Log.d(
                    TAG,
                    "On scan device:" + result.device.name + " address: " + result.device.address
                )
                val bleCompatScannerUtil = this@BleCompatScannerUtil
                val device = result.device
                bleCompatScannerUtil.onDevice(device)
            }

            override fun onScanFailed(i: Int) {
                Log.d(TAG, "On scan failed")
            }
        }
    }

    fun onDevice(device: BluetoothDevice?) {
        var device = device
        if (!Intrinsics.areEqual(device!!.address, this.targetAddress)) {
            device = null
        }
        if (device != null) {
//            BuildersKt__Builders_commonKt.launch$default(XossCoroutineScopeKt.getXossCoroutineScope(), null, null, new BleCompatScannerUtil$onDevice$2$1(this, device, null), 3, null);
        }
    }

    fun scan() {
//        BuildersKt__Builders_commonKt.launch$default(XossCoroutineScopeKt.getXossCoroutineScope(), null, null, new BleCompatScannerUtil$scan$1(this, null), 3, null);
    }

    fun cancel() {
        try {
            scanner.stopScan(this.scanCallback)
        } catch (unused: Exception) {
        }
    }

    open inner class BleScanCallback : ScanCallback() {
        val scannerUtil: BluetoothLeScannerCompat = scanner

        override fun onScanFailed(param1Int: Int) {
            Log.d("BleManagerScanUtil", "On scan failed")
        }

        override fun onScanResult(param1Int: Int, param1ScanResult: ScanResult) {
            val stringBuilder = StringBuilder()
            stringBuilder.append("On scan device:")
            stringBuilder.append(param1ScanResult.device.name)
            stringBuilder.append(" address: ")
            stringBuilder.append(param1ScanResult.device.address)
            Log.d("BleManagerScanUtil", stringBuilder.toString())
            val bleCompatScannerUtil = this@BleCompatScannerUtil
            val bluetoothDevice = param1ScanResult.device
            bleCompatScannerUtil.onDevice(bluetoothDevice)
        }
    }

    companion object {
        private const val TAG = "BleCompatScannerUtil"
    }
}
